package com.zy.quartz.task;

import cn.hutool.core.util.StrUtil;
import com.zy.business.domain.TbAbsence;
import com.zy.business.domain.TbPunchCard;
import com.zy.business.mapper.AbsenceMapper;
import com.zy.business.mapper.HolidayMapper;
import com.zy.business.service.HolidayService;
import com.zy.business.service.PunchCardService;
import com.zy.business.service.ShiftService;
import com.zy.common.core.domain.entity.SysUser;
import com.zy.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description: 定时打卡提醒
 * @author: whf
 * @create: 2023-02-07 10:42
 **/
@Component("remindTask")
public class RemindTask {

    @Autowired
    private PunchCardService punchCardService;

    @Resource
    private SysUserMapper userMapper;

    @Autowired
    private HolidayService holidayService;

    @Resource
    private HolidayMapper holidayMapper;

    @Resource
    private AbsenceMapper absenceMapper;

    @Autowired
    private ShiftService tbShiftService;

    public void sendRemind1(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1) {
            List<TbPunchCard> punchCards = punchCardService.getByStatus(0, 1,1);
            String startTime1 = tbShiftService.selectTbShiftById().getStartTime1();
            String remark = "上班时间为"+startTime1+"，请别忘记打卡！";
            if (punchCards != null && punchCards.size() > 0) {
                for (TbPunchCard punchCard : punchCards) {
                    SysUser user = userMapper.selectUserById(punchCard.getUserId());
                    String timeStr = sdf.format(new Date())+ " "+startTime1;
                    List<TbAbsence> absenceList = absenceMapper.selectListByTime(user.getUserId(),timeStr);
                    if (absenceList!=null && absenceList.size()>0){
                        punchCard.setStatus(3);
                        punchCard.setUpdateTime(new Date());
                        punchCardService.updateById(punchCard);
                    }else {
                        if(StrUtil.isNotBlank(user.getOpenId())){
                            punchCardService.sendMessage(user.getOpenId(), remark);
                        }
                    }
                }
            }
        }
    }

    public void sendRemind2(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1) {
            String endTime1 = tbShiftService.selectTbShiftById().getEndTime1();
            String remark = "下班时间为"+endTime1+"，请别忘记打卡！";
            List<TbPunchCard> punchCards = punchCardService.getByStatus(0, 2,1);
            if (punchCards != null && punchCards.size() > 0) {
                for (TbPunchCard punchCard : punchCards) {
                    SysUser user = userMapper.selectUserById(punchCard.getUserId());
                    String timeStr = sdf.format(new Date())+ " "+endTime1;
                    List<TbAbsence> absenceList = absenceMapper.selectListByTime(user.getUserId(),timeStr);
                    if (absenceList!=null && absenceList.size()>0){
                        punchCard.setStatus(3);
                        punchCard.setUpdateTime(new Date());
                        punchCardService.updateById(punchCard);
                    }else {
                        if(StrUtil.isNotBlank(user.getOpenId())){
                            punchCardService.sendMessage(user.getOpenId(), remark);
                        }
                    }
                }
            }
        }
    }

    public void sendRemind3(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1) {
            String startTime2 = tbShiftService.selectTbShiftById().getStartTime2();
            String remark = "上班时间为"+startTime2+"，请别忘记打卡！";
            List<TbPunchCard> punchCards = punchCardService.getByStatus(0, 3,1);
            if (punchCards != null && punchCards.size() > 0) {
                for (TbPunchCard punchCard : punchCards) {
                    SysUser user = userMapper.selectUserById(punchCard.getUserId());
                    String timeStr = sdf.format(new Date())+ " "+startTime2;
                    List<TbAbsence> absenceList = absenceMapper.selectListByTime(user.getUserId(),timeStr);
                    if (absenceList!=null && absenceList.size()>0){
                        punchCard.setStatus(3);
                        punchCard.setUpdateTime(new Date());
                        punchCardService.updateById(punchCard);
                    }else {
                        if(StrUtil.isNotBlank(user.getOpenId())){
                            punchCardService.sendMessage(user.getOpenId(), remark);
                        }
                    }
                }
            }
        }
    }

    public void sendRemind4(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1) {
            String endTime2 = tbShiftService.selectTbShiftById().getEndTime2();
            String remark = "下班时间为"+endTime2+"，请别忘记打卡！";
            List<TbPunchCard> punchCards = punchCardService.getByStatus(0, 4,1);
            if (punchCards != null && punchCards.size() > 0) {
                for (TbPunchCard punchCard : punchCards) {
                    SysUser user = userMapper.selectUserById(punchCard.getUserId());
                    String timeStr = sdf.format(new Date())+ " "+endTime2;
                    List<TbAbsence> absenceList = absenceMapper.selectListByTime(user.getUserId(),timeStr);
                    if (absenceList!=null && absenceList.size()>0){
                        punchCard.setStatus(3);
                        punchCard.setUpdateTime(new Date());
                        punchCardService.updateById(punchCard);
                    }else {
                        if(StrUtil.isNotBlank(user.getOpenId())){
                            punchCardService.sendMessage(user.getOpenId(), remark);
                        }
                    }
                }
            }
        }
    }

}
